package br.com.bankai.koby.service.impl;

import javax.persistence.NoResultException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.bankai.koby.repository.impl.ConfiguracaoRepository;
import br.com.bankai.koby.service.exception.NoResultFoundException;

/**
 * 
 * @author Leonardo
 *
 */
@Service
public class ConfiguracaoService extends CachedService {

	private static final String CACHE_NAME = "configuracoes";

	@Autowired
	private ConfiguracaoRepository repository;

	public String find(String codigo) throws NoResultFoundException {
		if (null == findOnCache(CACHE_NAME, codigo)) {
			try {
				String configuracao = repository.find(codigo);
				putOnCache(CACHE_NAME, codigo, configuracao);
				return configuracao;
			} catch (NoResultException e) {
				throw new NoResultFoundException("Configuracao nao encontrada: " + codigo);
			}
		} else {
			return (String) findOnCache(CACHE_NAME, codigo);
		}
	}
}